package com.my.blog.myblog.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.blog.myblog.entity.Resource;
import com.my.blog.myblog.service.ResourceService;
import com.my.blog.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;




/***
 *
 * @Auther: MuYun
 * @Date: 2021/8/26 9:38
 * @Description： 资源分享的controller
 *
 */
@RestController
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/selectLikeResourceListByPage")
    public ResultBean<IPage<Resource>> selectLikeResourceListByPage(Resource resource,
                                                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                                    @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize ){
        QueryWrapper<Resource> wrapper=new QueryWrapper<>();
        if(StrUtil.isNotBlank(resource.getTitle())){
            wrapper.lambda().eq(Resource::getTitle,resource.getTitle());
        }
        if(StrUtil.isNotBlank(resource.getContent())){
            wrapper.lambda().eq(Resource::getContent,resource.getContent());
        }
        if(ObjectUtil.isNotEmpty(resource.getStatus())){
            wrapper.lambda().eq(Resource::getStatus,resource.getStatus());
        }
        wrapper.lambda().orderByDesc(Resource::getAddTime);
        Page<Resource> resourcePage = resourceService.page(new Page<>(page, pageSize), wrapper);

        ResultBean<IPage<Resource>> resultBean=new ResultBean<>();
        if(ObjectUtil.isNotEmpty(resourcePage.getRecords())){
            resultBean.setData(resourcePage);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
